package com.wanho.qz103.hrsaas.sys.contoller;

import com.wanho.qz103.hrsaas.commons.annontations.Autowired;
import com.wanho.qz103.hrsaas.commons.annontations.GetMapping;
import com.wanho.qz103.hrsaas.commons.annontations.PutMapping;
import com.wanho.qz103.hrsaas.commons.annontations.RestController;
import com.wanho.qz103.hrsaas.commons.exception.ControllerException;
import com.wanho.qz103.hrsaas.commons.exception.ServiceException;
import com.wanho.qz103.hrsaas.commons.util.RequestUtils;
import com.wanho.qz103.hrsaas.commons.vo.R;
import com.wanho.qz103.hrsaas.sys.pojos.Employee;
import com.wanho.qz103.hrsaas.sys.pojos.Job;
import com.wanho.qz103.hrsaas.sys.service.EmployeeService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;

@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService ;
    /**
     * 保存员工个人信息
     * @param request
     * @return
     * @throws ControllerException
     */
    @PutMapping("/employees/{id}/personalInfo")
    public R modifyPersonalInfo(HttpServletRequest request) throws ControllerException {
        try {
            String[] ay = request.getRequestURI().split("/");
            String id = ay[ay.length-2] ;
            System.out.println("保存员工个人信息:" + id);
            Employee employee = new Employee();
            RequestUtils.parseRequest2Bean(request.getParameterMap(),employee);
            employee.setUserId(id);
            boolean modifyRs = employeeService.modify(employee) ;
            if (modifyRs) {
                return  R.success("保存员工个人信息详情成功！") ;
            }
            return  R.fail("保存员工个人信息详情失败！",employee) ;
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail("保存员工个人信息详情异常！") ;
        }
    }

    /**
     * 获取员工个人信息
     * @param request
     * @return
     * @throws ControllerException
     */
    @GetMapping("/employees/{id}/personalInfo")
    public R personalInfo(HttpServletRequest request) throws ControllerException {
        try {
            String[] ay = request.getRequestURI().split("/");
            String id = ay[ay.length-2] ;
            System.out.println("查看用户详情的id是:" + id);
            Employee employee = employeeService.getPersonalInfo(id);
            return  R.success("查询用户详情成功！",employee) ;
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail("查询用户详情异常！") ;
        }
    }

    /**
     * 获取员工岗位信息
     * @param request
     * @return
     * @throws ControllerException
     */
    @GetMapping("/employees/{id}/jobs")
    public R jobs(HttpServletRequest request) throws ControllerException {
        try {
            String[] ay = request.getRequestURI().split("/");
            String id = ay[ay.length-2] ;
            System.out.println("查看用户岗位的id是:" + id);
            Job job = employeeService.getJobByUserId(id);
            return  R.success("查询用户岗位成功！",job) ;
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail("查询用户岗位异常！") ;
        }
    }
}
